const { getSearchParams } = require("../modules/reqHelper");

function login(req, res) {
  // 提取查询参数 username+password
  let { username, password } = getSearchParams(req.url);
  getSearchParams;
  /* 处理登录逻辑 */
  if (username === "heige" && password === "123456") {
    // 写cookie logined=true; username=heige
    // 跳转首页
    res.writeHead(302, [
      ["Set-Cookie", "logined=true"],
      ["Set-Cookie", "username=heige"],
      ["Location", "/file/pages/home.html"],
    ]);
    res.end()
  } else {
    // 删除cookie logined
    // 跳转登录页
    res.writeHead(302, [
      [
        "Set-Cookie",
        "logined=iwontgotohell;expires=Thu Jan 01 1970 08:00:00 GMT+0800",
      ],
      ["Location", "/file/pages/login.html"],
    ]);
    res.end()
  }
}

module.exports = {
  login,
};
